In this Notebook, we learn how to build and solve systems of linear equations, and apply these techniques to solve practical problems.
Python is a general-purpose programming, but specialized features geared towards data analysis are available in various libraries, such as numpy. Numpy's main datastructure is the ndarray which supports opperations such as adding rows or columns of data in an element-wise fashion and performing math operations on multidimentionsl matrices. Numpy (and Nnarrays) are the subject of this notebook.
On the one hand, ndarrays are similar to lists in that they consist of a collection of items that can be accessed via indexes, but on the other hand, ndarrays are different from lists by being homogeneous and only containing objects of the same type (list can be heterogeneous).
To work with ndarrays, we need to load the numpy library.
In [1]:
import numpy as np
We can create an ndarray by passing a list to the np.array() function:
In [2]:
list1 = [1, 2, 3, 4, 5] # Define a list
In [3]:
array1 = np.array(list1) # Pass the list to np.array()
In [4]:
type(array1) # Check the object's type
Out[4]:
In [5]:
print("array1 = ", array1) # Check the content of the array (printing in Python 3)
In [6]:
print ("array1 = %s" % np.array_str(array1)) # Check the content of the array (printing in Python 2)
To create an array with more than one dimension, we can pass a nested list to the np.array() function:
In [47]:
list2 = [[1,2,3,4,5], [6,7,8,9,10]]
array2 = np.array(list2)
print("array2 = ", array2) # Python 3
In [48]:
print("array2 = %s" % np.array_str(array2)) # Python 2 and 3
The parameters of an ndarray include the number of dimensions it has, the size of each dimension and the type of data it contains. We can check the dimensions of an ndarray with the shape attribute:
In [9]:
array2.shape
Out[9]:
The output above shows that array2 is a 2-dimensional array with 2 rows and 5 columns. We can check the size (total number of items) of an array with the size attribute and the type of the data it contains with the dtype attribute:
In [10]:
print("array2 has", array2.size ,"items of type", array2.dtype) # Python 3
In [11]:
print ("array2 has %d items of type %s" % (array2.size, array2.dtype)) # Python 2 and 3
Numpy has several functions for creating arrays, such as: np.identity(), to create a square 2d array with 1's across the diagonal and 0's everywhere else
In [12]:
np.identity(n = 3) # n is the size of the square 2-d array
Out[12]:
np.eye() to create a 2d array with 1's across a specified diagonal and 0's everywhere else
In [13]:
np.eye(3, # Number of rows
5, # Number of columns
1) # Index of the diagonal (main diagonal, 0, is the default)
Out[13]:
In [14]:
# np.ones() to create an array filled with ones:
np.ones(shape= [2,3])
Out[14]:
In [15]:
# np.zeros() to create an array filled with zeros:
np.zeros(shape= [3,4])
Out[15]:
In [16]:
d_array = np.array([1,2,3,4,5,6])
d_array[2] # Get the item at index 2
Out[16]:
In [17]:
d_array[4:] # Get a slice from index 3 to the end
Out[17]:
In [18]:
d_array[::-1] # shortcut to reverse the array
Out[18]:
If an ndarray has more than one dimension, separate indexes for each dimension with a comma:
In [19]:
# Create a new two dimensional array
dd_array = np.array([d_array, d_array + 10, d_array + 100])
print(dd_array)
In [20]:
# Get the element on row 2, and column 3
dd_array[2, 3]
Out[20]:
In [21]:
# Slice elements starting at row 1, and column 3
dd_array[1:, 4:]
Out[21]:
In [22]:
#Reverse the array in both dimensions (rotation)
dd_array[::-1, ::-1]
Out[22]:
In [23]:
np.reshape(dd_array, # Array to reshape
newshape=(2,9)) # Dimensions of the new array
Out[23]:
Unravel a multi-dimensional into 1 dimension with np.ravel():
In [24]:
np.ravel(dd_array, # Array to reshape
order='C') # Unravel by rows
Out[24]:
In [25]:
np.ravel(dd_array,
order='F') # Unravel by columns
Out[25]:
In [26]:
dd_array.flatten() #flatten a multi-dimensional array into 1 dimension and return a copy of the result
Out[26]:
In [27]:
dd_array.T #get the transpose
Out[27]:
In [28]:
np.flipud(dd_array) #Flip an array vertically
Out[28]:
In [29]:
np.fliplr(dd_array) #Flip an array horizontally
Out[29]:
In [30]:
np.rot90(dd_array, # Rotate the array 90 degrees counter-clockwise
k=1) # Number of 90 degree rotations
Out[30]:
In [31]:
np.roll(dd_array, # Shift elements in an array along a given dimension
shift = 2, # Shift elements 2 positions
axis = 1) # In each row
Out[31]:
In [32]:
np.roll(dd_array, #Leave the axis argument empty to shift across all dimensions
shift = 2)
Out[32]:
In [33]:
#Join arrays along an axis
array_to_join = np.array([[10,20,30],[40,50,60],[70,80,90]])
np.concatenate((dd_array,array_to_join), # Arrays to join
axis=1) # Axis to join upon
Out[33]:
In [34]:
dd_array + 10 # Add 10 to each element
Out[34]:
In [35]:
dd_array - 10 # Subtract 10 from each element
Out[35]:
In [36]:
dd_array * 2 # Multiply each element by 2
Out[36]:
In [37]:
dd_array ** 2 # Square each element
Out[37]:
One can also use the basic math operators on two arrays with the same shape. The basic math operators function in an element-wise fashion, returning an array with the same shape as the original.
In [38]:
array3 = np.array([[1,2],[3,4]])
array3 + array3
Out[38]:
In [39]:
array3 - array3
Out[39]:
In [40]:
array3 * array3
Out[40]:
Numpy also provides math functions for ndarrays such as:
In [41]:
np.mean(dd_array) # The mean of all the elements in an array
Out[41]:
In [42]:
np.std(dd_array) #Get the standard deviation all the elements in an array
Out[42]:
In [43]:
np.sum(dd_array,
axis=1) # Get the row sums for the elements of an array
Out[43]:
In [44]:
np.sum(dd_array,
axis=0) # Get the column sums
Out[44]:
In [45]:
np.sqrt(dd_array) # Take the square root of each element in the array
Out[45]:
T np.dot() returns the dot product of two arrays.
In [46]:
np.dot(dd_array[0,0:], # Slice row 0
dd_array[1,0:]) # Slice row 1
Out[46]:
The numpay package also includes a variety of more advanced linear algebra functions, such as computing eigenvectors and eigenvalues or inverting matrices.